/**
 * 
 */
package us.gaaoc.framework.util.db;


import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import us.gaaoc.framework.dao.ModelFacade;
import us.gaaoc.framework.model.CaseRecord;

/**
 * @author artripa
 *
 */
@Component
@Configurable
public class CaseRecordSystemValidator {

	private Logger log = Logger.getLogger(CaseRecordSystemValidator.class);

	
	@Autowired
	SessionFactory sessionFactory;
	
	@Transactional(readOnly=true)
	public void validateAll()
	{
		System.out.println("getting session");
		Session s = sessionFactory.getCurrentSession();
		
		System.out.println("running query");
		Iterator<CaseRecord> iter = s.createQuery("from CaseRecord where deletedFilingFlag = 0 and clearFilingFlag = 0")
			.iterate();
		
		int valid = 0;
		
		System.out.println("running");
		while(iter.hasNext())
		{
			CaseRecord cr = iter.next();
			
			if(cr.getDefendants().isEmpty())
			{
				System.out.println("bad case (no defendants) " + cr.getUuid());
				continue;
			}
			
			
			if(cr.getPlaintiffs().isEmpty())
			{
				System.out.println("bad case (no plaintiffs) " + cr.getUuid());
				continue;
			}
			
			if(cr.getCaseRecordHistory().isEmpty())
			{
				System.out.println("bad case (no events)" + cr.getUuid());
				continue;
			}
			
			valid++;
		}
		
		System.out.println(valid);
		
	}
	
	
	public static void main(String[] args)
	{
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
			"classpath:applicationContext-gaje2.xml");
		
		CaseRecordSystemValidator v = (CaseRecordSystemValidator) context.getBean("caseRecordSystemValidator");
		v.validateAll();
		
	}
	
	/**
	 * @param sessionFactory the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	
	
}
